package app.android.com.wuandroidapp.test.view.fragment;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import app.android.com.wuandroidapp.AndroidApplication;
import app.android.com.wuandroidapp.R;
import app.android.com.wuandroidapp.library.fragment.BaseFragment;
import app.android.com.wuandroidapp.test.contract.HomePageContract;
import app.android.com.wuandroidapp.test.model.db.entities.minimalist.Weather;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link HomePageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomePageFragment extends BaseFragment implements HomePageContract.View {


    private static String usnames;
    @BindView(R.id.tv_textview)
    TextView tvTextview;

    private OnFragmentInteractionListener mListener;
    Unbinder unbinder;

    private HomePageContract.Presenter presenter;
    public HomePageFragment() {
        // Required empty public constructor
    }
    public static HomePageFragment newInstance(String username) {
        usnames=username;
        return new HomePageFragment();
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_home_page, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void setPresenter(HomePageContract.Presenter presenter) {
        this.presenter=presenter;
    }

    @Override
    public void onResume() {
        super.onResume();

//        getSmsFromPhone();
        assert presenter != null;
        presenter.subscribe();
    }
    @Override
    public void onPause() {
        super.onPause();
        presenter.unSubscribe();
    }
    @Override
    public void displayWeatherInformation(Weather weather) {
        tvTextview.setText(usnames+weather.getCityName()+arrayList.size());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void updatePageTitle(String title);
    }

    private Uri SMS_INBOX = Uri.parse("content://sms/");

    ArrayList<String> arrayList=new ArrayList<>();
    public void getSmsFromPhone() {
        ContentResolver cr = AndroidApplication.getInstance().getContentResolver();
        String[] projection = new String[]{"body"};//"_id", "address", "person",, "date", "type
        Cursor cur = cr.query(SMS_INBOX, projection, null, null, "date desc");
        if (null == cur)
            return;
        if (cur.moveToNext()) {
            String number = cur.getString(cur.getColumnIndex("address"));//手机号
            String name = cur.getString(cur.getColumnIndex("person"));//联系人姓名列表
            String body = cur.getString(cur.getColumnIndex("body"));
            arrayList.add(number);
        }
    }
}
